package honeypot.wljkzxt.action;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;

import cm.util.hibernate.HibernateSessionFactory;

import com.opensymphony.xwork2.ActionSupport;

public class NetPieAction extends ActionSupport {
	
	private double bd_num;
	private double zc_num;
	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
    private Session session_1;
    private Transaction trans_1;
    private String hql_1;
    private Query query_1;
    private List l_1;
    private Date begindate;
    private Date enddate;
	public double getBd_num() {
		return bd_num;
	}
	public void setBd_num(double bdNum) {
		bd_num = bdNum;
	}
	public double getZc_num() {
		return zc_num;
	}
	public void setZc_num(double zcNum) {
		zc_num = zcNum;
	}
	public Session getSession() {
		return session;
	}
	public void setSession(Session session) {
		this.session = session;
	}
	public Transaction getTrans() {
		return trans;
	}
	public void setTrans(Transaction trans) {
		this.trans = trans;
	}
	public String getHql() {
		return hql;
	}
	public void setHql(String hql) {
		this.hql = hql;
	}
	public Query getQuery() {
		return query;
	}
	public void setQuery(Query query) {
		this.query = query;
	}
	public List getL() {
		return l;
	}
	public void setL(List l) {
		this.l = l;
	}
	public Session getSession_1() {
		return session_1;
	}
	public void setSession_1(Session session_1) {
		this.session_1 = session_1;
	}
	public Transaction getTrans_1() {
		return trans_1;
	}
	public void setTrans_1(Transaction trans_1) {
		this.trans_1 = trans_1;
	}
	public String getHql_1() {
		return hql_1;
	}
	public void setHql_1(String hql_1) {
		this.hql_1 = hql_1;
	}
	public Query getQuery_1() {
		return query_1;
	}
	public void setQuery_1(Query query_1) {
		this.query_1 = query_1;
	}
	public List getL_1() {
		return l_1;
	}
	public void setL_1(List l_1) {
		this.l_1 = l_1;
	}
	public Date getBegindate() {
		return begindate;
	}
	public void setBegindate(Date begindate) {
		this.begindate = begindate;
	}
	public Date getEnddate() {
		return enddate;
	}
	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	public String execute() throws Exception
	{
		SimpleDateFormat yue = new SimpleDateFormat( 
		  "yyyy年MM月dd日"); 
		SimpleDateFormat r = new SimpleDateFormat( 
		  "yyyy-MM-dd"); 
		String begin=r.format(begindate);
		String end=r.format(enddate);
		session=HibernateSessionFactory.getSession();
		trans=session.beginTransaction();
		hql="select count(*) from ScanInfo as scan where scan.status<>:t AND scan.time between '"+begin+"' and '"+end+"'";
		query=session.createQuery(hql);
		query.setString("t","OK");//设置查询参数
		session_1=HibernateSessionFactory.getSession();
		trans_1=session_1.beginTransaction();
		hql_1="select count(*) from ScanInfo as scan where scan.status=:t AND scan.time between '"+begin+"' and '"+end+"'";
		query_1=session_1.createQuery(hql_1);
		query_1.setString("t","OK");//设置查询参数
		bd_num=Double.parseDouble(query.iterate().next().toString());
		zc_num=Double.parseDouble(query_1.iterate().next().toString());
		System.out.println("病毒"+"的数量为"+(bd_num));
		System.out.println("正常文件"+"的数量为"+(zc_num));
		
//下面为制作饼图的代码
		
		  String y1 = yue.format(begindate); 
		  String y2 = yue.format(enddate);
		DefaultPieDataset dataset= new DefaultPieDataset();
        dataset.setValue("病毒", bd_num);
		dataset.setValue("正常文件",zc_num);
  JFreeChart chart=ChartFactory.createPieChart(y1+"  至  "+y2, dataset, true, false, false);
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
		PiePlot plot=(PiePlot)chart.getPlot();
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
				"{0}={1}({2})", NumberFormat.getNumberInstance(), 
				new DecimalFormat("0.00%"))); 
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
		"{0}={1}({2})",NumberFormat.getNumberInstance(), 
		new DecimalFormat("0.00%"))); 
		plot.setLabelFont(new Font("宋体",Font.BOLD,10));
		plot.setBackgroundPaint(Color.WHITE);
		plot.setBackgroundImageAlpha(0.65f);
		plot.setForegroundAlpha(0.65f); 
		plot.setCircular(false, false); 

		LegendTitle legend=chart.getLegend();
		legend.setItemFont(new Font("宋体",Font.BOLD,10));
		
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
					800, 500);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
       
		//提交事务
		session.close();
		//session_user.close();
		System.out.println("成功");
	
		return null;
	}
	
}
